package com.qf.schedule;

import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qf.config.RedisKey;
import com.qf.dao.MedicleSeckillDrugMapper;

import com.qf.pojo.entity.MedicleSeckilldrug;

import com.qf.util.DateUtil;

import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

/**
 * @ClassName findKillDate
 * @Description
 * @Author lanzhike
 * @Date 2023/7/27 11:37
 * @Version 1.0
 * 初始化所有秒杀时间段
 */
@Component
public class SeckillDrugSchedule {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Resource
    MedicleSeckillDrugMapper seckillDrugMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0/10 * * * * ?")
    public void initKillProducts(){
//        applicationStartUp.init();
        Boolean lock = stringRedisTemplate.opsForValue().setIfAbsent(RedisKey.SCHEDLE_INITIAL_KEY, "lock");
        if (lock){
//            获取当前时间后的时间段
            List<Date> dateMenus = DateUtil.getDateMenus();
            for (Date date : dateMenus) {
                String key = DateUtil.date2Str(date);
//                当前时间段的起始时间
//                Date date1 = new Date();
//                TimeZone timeZone = TimeZone.getDefault();
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                simpleDateFormat.setTimeZone(timeZone);
//                String startTime =simpleDateFormat.format(date1);
                String startTime =new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//                当前时间段2小时后的结束时间
                Date end = DateUtil.addDateHour(date, 2);
                String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end);
//                查询数据库符合条件的商品
                QueryWrapper<MedicleSeckilldrug> wrapper = new QueryWrapper<>();
//                状态为1,库存大于0,起始时间在当前起始时间后,结束时间在当前结束时间前
                wrapper.eq("seckill_status",1)
                        .gt("seckill_drugnum",0)
                        .ge("start_time",startTime)
                        .le("end_time",endTime);
//                排除已经存储过的商品
                Set keys = redisTemplate.boundHashOps(RedisKey.KILL_DRUG_KEY + key).keys();
                if (keys.size() > 0 && keys != null){
                    wrapper.notIn("drug_id",keys);
                }

                List<MedicleSeckilldrug> goods = seckillDrugMapper.selectList(wrapper);
//                将查询到符合条件的商品存到redis中,采用hash
                for (MedicleSeckilldrug good : goods) {
                    System.out.println("-------------------->存储数据");
                    redisTemplate.boundHashOps(RedisKey.KILL_DRUG_KEY+key).put(good.getDrugId().toString(),good);
                    stringRedisTemplate.opsForValue().set(RedisKey.KILL_DRUG_NUMBER+key+":"+good.getDrugId().toString(), good.getSeckillDrugnum().toString());
                }
            }
//            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            stringRedisTemplate.delete(RedisKey.SCHEDLE_INITIAL_KEY);
        }
        System.out.println("释放锁");
    }
}
